package com.patui.Ui;

import java.io.IOException;
import java.net.MalformedURLException;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.text.TextPaint;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.patui.Logic.TotalActivity;
import com.patui.Logic.FrameSevice;
import com.patui.MyView.ImageBtn;
import com.patui.PatuiMe.R;
import com.patui.Weibo.AccessToken;
import com.patui.Weibo.Weibo;
import com.patui.Weibo.WeiboException;
import com.patui.Weibo.WeiboParameters;
public class LoginLayout extends RelativeLayout{
   public final static String TAG="@___Patui.LoginLayout";
   private ImageView mImgTitleBarBg=null;
   private ImageBtn mImgbackBtn=null;
   private ImageBtn mImgLoginBtn=null;
  //private ImageView mImgResigerBtn=null;
   private EditText  mEtxtUser=null;
   private EditText  mEtxtPsw=null;

   final static int ID_TOP_TITLE_FLAYOUT=1;
   final static int ID_TEXT_ACCOUT=2;
   final static int ID_EDITTEXT_ACCOUT=3;
   final static int ID_TEXT_PSW=4;
   final static int ID_EDITTEXT_PSW=5;
   final static int ID_BTN_LOGIN=6;


	public LoginLayout(Context context) {
		super(context);
		initLayout(context);
		// TODO Auto-generated constructor stub
	}

	private void initLayout(Context context) {
		// TODO Auto-generated method stub
		int[] colors = new int[2];
		colors[0] = 0xFFF4F4F4;
		colors[1] = 0xffDED9D8;
		GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
		this.setBackgroundDrawable(drawable);
		
		LayoutParams relativeParams=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
		relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		relativeParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		FrameLayout topTileFlayout=new FrameLayout(context);
		this.addView(topTileFlayout, relativeParams);
		topTileFlayout.setId(ID_TOP_TITLE_FLAYOUT);
		
		
		FrameLayout.LayoutParams frameParams=new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
		frameParams.gravity=Gravity.LEFT;
		mImgTitleBarBg=new ImageView(context);
		mImgTitleBarBg.setImageResource(R.drawable.loginactivity_titlebar_bg_nor);
		mImgTitleBarBg.setScaleType(ScaleType.FIT_XY);
		topTileFlayout.addView(mImgTitleBarBg,frameParams);
		
		frameParams=new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		frameParams.gravity=Gravity.LEFT|Gravity.CENTER_VERTICAL;
		mImgbackBtn=new ImageBtn(context,R.drawable.loginactivity_title_back,R.drawable.loginactivity_title_back_ed);
		mImgbackBtn.setScaleType(ScaleType.CENTER_INSIDE);
		topTileFlayout.addView(mImgbackBtn,frameParams);
		
		frameParams=new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		frameParams.gravity=Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL;
		TextView mTxtWeibo=new TextView(context);
		mTxtWeibo.setTextColor(0xff000000);
		mTxtWeibo.setTextSize(18.0f);
		TextPaint mtPaint=mTxtWeibo.getPaint();
		mtPaint.setFakeBoldText(true);
		mTxtWeibo.setText("Weibo");
		topTileFlayout.addView(mTxtWeibo,frameParams);
	
		
	    relativeParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	    relativeParams.addRule(RelativeLayout.BELOW,ID_TOP_TITLE_FLAYOUT);
	    relativeParams.setMargins(20, 20, 0, 0);
	    TextView mTxtAccount=new TextView(context);
	    mTxtAccount.setTextColor(0xff000000);
	    mTxtAccount.setTextSize(18.0f);
		mtPaint=mTxtAccount.getPaint();
		mtPaint.setFakeBoldText(true);
		mTxtAccount.setText("Account");
		this.addView(mTxtAccount, relativeParams);
		mTxtAccount.setId(ID_TEXT_ACCOUT);
		
		
	    relativeParams=new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
	    relativeParams.addRule(RelativeLayout.BELOW,ID_TEXT_ACCOUT);
	    relativeParams.setMargins(20, 2, 20, 0);
	    mEtxtUser=new EditText(context);
		this.addView(mEtxtUser, relativeParams);
		mEtxtUser.setId(ID_EDITTEXT_ACCOUT);
		
		
	    relativeParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	    relativeParams.addRule(RelativeLayout.BELOW,ID_EDITTEXT_ACCOUT);
	    relativeParams.setMargins(20, 8, 0, 0);
	    TextView mTxtPsw=new TextView(context);
	    mTxtPsw.setTextColor(0xff000000);
	    mTxtPsw.setTextSize(18.0f);
		mtPaint=mTxtPsw.getPaint();
		mtPaint.setFakeBoldText(true);
		mTxtPsw.setText("PassWord");
		this.addView(mTxtPsw, relativeParams);
		mTxtPsw.setId(ID_TEXT_PSW);
		
		
	    relativeParams=new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
	    relativeParams.addRule(RelativeLayout.BELOW,ID_TEXT_PSW);
	    relativeParams.setMargins(20, 2, 20, 0);
	    mEtxtPsw=new EditText(context);
		this.addView(mEtxtPsw, relativeParams);
		mEtxtPsw.setId(ID_EDITTEXT_PSW);
		
	    relativeParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	    relativeParams.addRule(RelativeLayout.BELOW,ID_EDITTEXT_PSW);
	    relativeParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
	    relativeParams.topMargin=25;
	    
	    FrameLayout mImgTextLayout=new FrameLayout(context);
	    this.addView(mImgTextLayout,relativeParams);
	    mImgTextLayout.setId(ID_BTN_LOGIN);
	    mImgTextLayout.setOnClickListener(mOnClickListener);
	    
	    frameParams=new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	    frameParams.gravity=Gravity.LEFT;
	    mImgLoginBtn=new ImageBtn(context,R.drawable.loginactivity_btn_login,R.drawable.loginactivity_btn_login_ed);
	    mImgLoginBtn.setScaleType(ScaleType.CENTER_INSIDE);
	    mImgTextLayout.addView(mImgLoginBtn, frameParams);
		
	    frameParams=new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	    frameParams.gravity=Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL;
		TextView mTxtxLogin=new TextView(context);
		mTxtxLogin.setTextColor(0xffffffff);
		mTxtxLogin.setTextSize(18.0f);
		mtPaint=mTxtxLogin.getPaint();
		mtPaint.setFakeBoldText(true);
		mTxtxLogin.setText("Login");
		mImgTextLayout.addView(mTxtxLogin,frameParams);
		
	    relativeParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	    relativeParams.addRule(RelativeLayout.BELOW,ID_BTN_LOGIN);
	    relativeParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
	    relativeParams.topMargin=15;
	    ImageView mImgSplitLine=new ImageView(context);
	    mImgSplitLine.setImageResource(R.drawable.loginactivity_group_divider);
		this.addView(mImgSplitLine, relativeParams);
		
		}
	  private OnClickListener mOnClickListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case ID_BTN_LOGIN:
				//FrameSevice.addStartActivityTask(TotalActivity.MainTabActivity);
				FrameSevice.addLoginTask(1);
				
				break;

			default:
				break;
			}
			// TODO Auto-generated method stub
			
		}
	};
	public void getFriendTimeline() 
	throws MalformedURLException, IOException, WeiboException{
	Weibo mWeibo=Weibo.getInstance();
	mWeibo.setAccessToken(new AccessToken("94e5c330957127b3bc973ae5b8fe997c", "f7bf53702e8bd8a3aa65cee425b468e5"));
	String url = Weibo.SERVER + "statuses/friends_timeline.json";
	WeiboParameters bundle = new WeiboParameters();
	bundle.add("source", Weibo.APP_KEY);
	String rlt = mWeibo.request(getContext(), url, bundle, "GET", mWeibo.getAccessToken());
	System.out.println(rlt);
	}


}
